package com.yiji.fintech.musfinance.common.eventbus.listener;

import com.acooly.core.utils.enums.ResultStatus;
import com.google.common.eventbus.Subscribe;
import com.yiji.fintech.musfinance.base.enums.NotifyStatusEnum;
import com.yiji.fintech.musfinance.base.enums.TransStatusEnum;
import com.yiji.fintech.musfinance.center.base.result.ResultFacade;
import com.yiji.fintech.musfinance.common.annotation.ListenerAnnotation;
import com.yiji.fintech.musfinance.common.context.SpringApplicationContext;
import com.yiji.fintech.musfinance.common.domain.SignDomain;
import com.yiji.fintech.musfinance.common.domain.base.BaseDomain;
import com.yiji.fintech.musfinance.common.eventbus.event.SignEvent;
import com.yiji.fintech.musfinance.common.eventbus.listener.base.AbstractCommonListener;
import com.yiji.fintech.musfinance.common.strategy.base.UniqueBaseStrategy;
import com.yiji.fintech.musfinance.common.strategy.context.SendGeteWayContext;
import com.yiji.fintech.musfinance.common.strategy.context.UniqueContext;
import com.yiji.fintech.musfinance.common.strategy.gateway.SendGateWaySignStrategy;
import com.yiji.fintech.musfinance.entity.InstBank;
import com.yiji.fintech.musfinance.service.InstBankService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 签约监听器
 * Created by ouwen@yiji.com} on 2017/9/27.
 */
@ListenerAnnotation(value = "signListener")
@Scope("prototype")
@Service
public class SignListener extends AbstractCommonListener<SignEvent,SignDomain> {
    @Subscribe
    @Override
    public void doExcute(SignEvent event) {
        super.doExcute(event);
    }

    @Override
    public void unique(SignDomain domain, ResultFacade result) {
        UniqueBaseStrategy uniqueStrategy = (UniqueBaseStrategy) SpringApplicationContext.getBeanByName("signUniqueStrategy");
        UniqueContext uniqueContext = new UniqueContext(uniqueStrategy);
        uniqueContext.doUnique(domain,result);
    }

    @Override
    protected void sendgetway(BaseDomain domain, ResultFacade result) {
        SendGateWaySignStrategy signStrategy = (SendGateWaySignStrategy) SpringApplicationContext.getBeanByClass(SendGateWaySignStrategy.class);
        SendGeteWayContext context = new SendGeteWayContext(signStrategy);
        context.doSendGateWay(domain,result);
    }

    @Override
    protected void initTransaction(BaseDomain domain) {
        SignDomain signDomain = (SignDomain)domain;
        signDomain.setStatus(ResultStatus.processing);
        signDomain.setNotifyStatus(NotifyStatusEnum.NO);
        signDomain.setTransStatus(TransStatusEnum.INITIAL);
        signDomain.setGatewayTime(new Date());
        //初始化付款方银行信息
        InstBankService instBankService = (InstBankService)SpringApplicationContext.getBeanByClass(InstBankService.class);
        InstBank instBank = instBankService.unique(signDomain.getBankId());
        if(instBank != null){
            signDomain.setBankName(instBank.getInstBankName());
        }
    }
}
